package com.czl.elm.controller;

import com.czl.elm.service.IEstimateService;
import com.czl.elm.util.ActionResult;
import com.czl.elm.util.FileUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/estimate")
public class EstimateController {
    @Autowired
    private IEstimateService estimateService;
    @Value("${file.upload.url}")
    private String uploadFilePath;

    @ApiOperation("上传照片，测试用")
    @PostMapping("/upPhoto")
    public ActionResult upPhotos(@RequestPart("photo") MultipartFile[] file,@RequestParam("msg") String msg)throws Exception{

        System.out.println(msg);
        if (file != null && file.length >= 1) {
            int index = 0;
            for (MultipartFile profile : file) {
                String filetype = profile.getOriginalFilename().split("\\.")[1];
                //System.out.println(filetype);
                if (!FileUtil.imgCheck(filetype)) {
                    return ActionResult.error("文件类型不正确");
                }

                String path = "\\estimate\\" + index +System.currentTimeMillis() +"." + filetype;
                index++;
                File dest = new File(uploadFilePath + path);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    profile.transferTo(dest);
                } catch (Exception e) {
                    return ActionResult.error("上传文件失败");
                }
            }
        }else{
            return ActionResult.error("上传失败");
        }
        return ActionResult.success("上传成功");
    }

    @ApiOperation("上传评论")
    @PostMapping("/addEstimate")
    public ActionResult addEstimate(@RequestPart(value = "photoList",required = false) MultipartFile[] photoList,
                                    @RequestParam(value = "orderId")String orderId,
                                    @RequestParam(value="score")Integer score,
                                    @RequestParam("integral")Integer integral,
                                    @RequestParam(value="content")String content,
                                    @RequestParam("anonymous")Integer anonymous,
                                    @RequestParam(value="foodRecommond",required = false)String foodRecommond
                                    ){
        return estimateService.addEstimate(photoList,orderId,score,integral,content,anonymous,foodRecommond);

    }

    @ApiOperation("根据查看店铺的所有评论")
    @GetMapping("/getEstimateByShop")
    public ActionResult getEstimateByShop(Integer shopId, String type, Integer currentPage, Integer size){
        return estimateService.getEstimateByShop(shopId,type,currentPage,size);
    }

    @ApiOperation("根据查看店铺的所有评论数量")
    @GetMapping("/getEstimateByShopNum")
    public ActionResult getEstimateByShopNum(Integer shopId){
        return estimateService.getEstimateByShopNum(shopId);
    }

    @ApiOperation("获取所有标签")
    @GetMapping("/getTag")
    public ActionResult getTag(){
        return estimateService.getTag();
    }

    @ApiOperation("点赞")
    @GetMapping("/addUseful")
    public ActionResult addUseful(String estimateId){
        return estimateService.addUseful(estimateId);
    }

    @ApiOperation("取消点赞")
    @GetMapping("/removeUseful")
    public ActionResult removeUseful(String estimateId){
        return estimateService.removeUseful(estimateId);
    }


}
